package com.panshi.controller;

import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

@Controller
public class UserController {

    @Autowired
    private Producer producer;

    @GetMapping("/user_main")
    public String userMain(){
        return "user/user_main";
    }

    @GetMapping("/user_solo")
    public String userSolo(){
        return "user/user_right_particulars";
    }

    @GetMapping("/user_messages")
    public String userMessages(){
        return "user/user_messages";
    }




    /**
     * 验证码功能
     * @param response
     * @param session
     */
    @GetMapping("/user/kaptcha")
    public void getKaptcha(HttpServletResponse response, HttpSession session) {
        // 生成验证码
        String text = producer.createText();
        BufferedImage image = producer.createImage(text);
        //log.info("验证码已生成");
        // 将验证码存入session
       System.out.println(session.getAttribute("kaptcha"));
        session.setAttribute("kaptcha", text);
        System.out.println(text);
        // 将突图片输出给浏览器
        response.setContentType("image/png");
        try {
            OutputStream os = response.getOutputStream();
            ImageIO.write(image, "png", os);
        } catch (IOException e) {
            //log.error("响应验证码失败:{}" ,e.getMessage());
            System.out.println();
        }
    }




}
